//
//  SWGeneralPoetryCell.swift
//  ChildStory
//
//  Created by linhan on 2016/12/9.
//  Copyright © 2016年 Aiya. All rights reserved.
//

import Foundation
import UIKit
class SWGeneralPoetryCell: UITableViewCell
{
    weak var poetry:SWPoetry?
    
    private var _change:Bool = true
    private var _cellWidth:CGFloat = 0
    
    private var _titleLabel:UILabel = UILabel()
    private var _categoryLabel:UILabel = UILabel()
    
    init(style: UITableViewCellStyle, reuseIdentifier: String?, cellWidth:CGFloat)
    {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        _cellWidth = cellWidth
        setup()
    }
    
    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setup()
    {
        let leftMargin:CGFloat = 15
        let rightMargin:CGFloat = 15
        
        let categoryLabelWidth:CGFloat = 120
        let categoryLabelX:CGFloat = _cellWidth - rightMargin - categoryLabelWidth
        _categoryLabel.font = UIFont.systemFont(ofSize: 12)
        _categoryLabel.textAlignment = .right
        _categoryLabel.textColor = UIColor(hex: 0x2f3231)
        _categoryLabel.frame = CGRectMake(categoryLabelX, 9, categoryLabelWidth, 16)
        contentView.addSubview(_categoryLabel)
        
        let titleLabelX:CGFloat = leftMargin
        _titleLabel.font = UIFont.systemFont(ofSize: 17)
        _titleLabel.textColor = UIColor(hex: 0x2f3231)
        _titleLabel.frame = CGRectMake(titleLabelX, 9, categoryLabelX - titleLabelX, 21)
        contentView.addSubview(_titleLabel)
    }
    
    override func prepareForReuse()
    {
        super.prepareForReuse()
        _change = true
    }
    
    override func layoutSubviews()
    {
        super.layoutSubviews()
        
        if let poetry = poetry , _change
        {
            _change = false
            _titleLabel.text = poetry.name
            _categoryLabel.text = poetry.summary
        }
    }
    
}
